package com.jhgsys.internal.incentive.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.incentive.entity.IncentiveRewardConfig;
import com.jhgsys.internal.incentive.service.IIncentiveRewardConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 *  Controller
 *
 * @author Lujiachen
 * @date 2024-08-12 09:56:23
 */
@Slf4j
@Validated
@Controller
public class IncentiveRewardConfigController extends BaseController {

    @Autowired
    private IIncentiveRewardConfigService incentiveRewardConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardConfig")
    @RequiresPermissions("incentiveRewardConfig:view")
    public String incentiveRewardConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            IncentiveRewardConfig incentiveRewardConfig = incentiveRewardConfigService.findIncentiveRewardConfigByTenantId(getCurrentTenantId());
            if(incentiveRewardConfig!=null){
                model.addAttribute("incentiveRewardConfig",incentiveRewardConfig);
            }
        }
        return FebsUtil.view("incentive/incentiveRewardConfig/incentiveRewardConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardConfig/add")
    @RequiresPermissions("incentiveRewardConfig:add")
    public String incentiveRewardConfigAdd(){
        return FebsUtil.view("incentive/incentiveRewardConfig/incentiveRewardConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardConfig/update/{id}")
    @RequiresPermissions("incentiveRewardConfig:edit")
    public String incentiveRewardConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("incentiveRewardConfig", incentiveRewardConfigService.findIncentiveRewardConfigById(id));
        return FebsUtil.view("incentive/incentiveRewardConfig/incentiveRewardConfigEdit");
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @GetMapping("incentiveRewardConfig")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:view")
    public FebsResponse getAllIncentiveRewardConfigs(IncentiveRewardConfig incentiveRewardConfig) {
        return new FebsResponse().success().data(incentiveRewardConfigService.findIncentiveRewardConfigList(incentiveRewardConfig));
    }

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @GetMapping("incentiveRewardConfig/list")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:view")
    public FebsResponse incentiveRewardConfigList(QueryRequest request, IncentiveRewardConfig incentiveRewardConfig) {
        Map<String, Object> dataTable = getDataTable(this.incentiveRewardConfigService.findIncentiveRewardConfigPage(request, incentiveRewardConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 IncentiveRewardConfig
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @Log("新增IncentiveRewardConfig")
    @PostMapping("incentiveRewardConfig")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:edit")
    public FebsResponse addIncentiveRewardConfig(@RequestBody IncentiveRewardConfig incentiveRewardConfig) throws FebsException {
        checkNecessaryColumns(incentiveRewardConfig,false);
        try {
            incentiveRewardConfig.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveRewardConfigService.createIncentiveRewardConfig(incentiveRewardConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增IncentiveRewardConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveRewardConfig
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @Log("逻辑删除IncentiveRewardConfig")
    @GetMapping("incentiveRewardConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:delete")
    public FebsResponse deleteIncentiveRewardConfig(@PathVariable("id") String incentiveRewardConfigIds) throws FebsException {
        try {
            IncentiveRewardConfig incentiveRewardConfig = new IncentiveRewardConfig();
            incentiveRewardConfig.setDeletedBy(getCurrentUser().getUserName());
            incentiveRewardConfig.setDeletedTime(new Date());
            incentiveRewardConfig.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveRewardConfigService.deleteIncentiveRewardConfig(incentiveRewardConfig,incentiveRewardConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 IncentiveRewardConfig
    * @author Lujiachen
    * @date 2024-08-12 09:56:23
    */
    @Log("物理删除IncentiveRewardConfig")
    @GetMapping("incentiveRewardConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:delete")
    public FebsResponse physicallyDeleteIncentiveRewardConfig(@PathVariable("id") String incentiveRewardConfigIds) throws FebsException {
        try {
            this.incentiveRewardConfigService.physicallyDeleteIncentiveRewardConfig(incentiveRewardConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除IncentiveRewardConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 IncentiveRewardConfig
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @Log("修改IncentiveRewardConfig")
    @PostMapping("incentiveRewardConfig/update")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:edit")
    public FebsResponse updateIncentiveRewardConfig(@RequestBody IncentiveRewardConfig incentiveRewardConfig) throws FebsException {
        checkNecessaryColumns(incentiveRewardConfig,true);
        try {
            incentiveRewardConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveRewardConfigService.updateIncentiveRewardConfig(incentiveRewardConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改IncentiveRewardConfig失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


    /**
     * 查询（单个）
     * @author Lujiachen
     * @date 2024-08-12 09:56:23
     */
    @GetMapping("incentiveRewardConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardConfig:view")
    public FebsResponse getIncentiveRewardConfigById(Model model, @PathVariable("id") String  id) {
        IncentiveRewardConfig incentiveRewardConfig = incentiveRewardConfigService.findIncentiveRewardConfigByTenantId(id);
        model.addAttribute("incentiveRewardConfig", incentiveRewardConfig);
        return new FebsResponse().success().data(model);
    }


    /**
     * 新增保存时，字段必填项校验
     * @author zhangwei
     * @date 2024-08-30 14:19:50
     */
    private void checkNecessaryColumns(IncentiveRewardConfig incentiveRewardConfig,boolean editFlg) throws FebsException {
        if(editFlg && StringUtils.isBlank(incentiveRewardConfig.getRewardConfigId())){
            throw new FebsException("主键不能为空");
        }
        else if(StringUtils.isBlank(incentiveRewardConfig.getTenantId())){
            throw new FebsException("所属公司Id不能为空");
        } else if(null == incentiveRewardConfig.getRewardAmountTax()){
            throw new FebsException("必填参数不能为空");
        } else if(null == incentiveRewardConfig.getScoreAmountRatio()){
            throw new FebsException("必填参数不能为空");
        }
    }
}
